
/*
 * 初始多个图片类型上传方法
 * initFileArry 是一个二维数组里面放置元素的顺序:上传文件对像,文件放置列表对象,上传最大数,文件类型
 */
function initFilesInput(initFileArry){
	for(var i=0;i<initFileArry.length;i++){
		/* Create an instance of the multiSelector class, pass it the output target and the max number of files */
		var multi_selector = new MultiSelector(initFileArry[i][1],initFileArry[i][2],initFileArry[i][3],initFileArry[i][0].id);
		/*Pass in the file element*/
		multi_selector.addElement(initFileArry[i][0]);
	}
}

function deleteImageDiv(parentId,divId,maxSize){
	var vid = document.getElementById(divId);
	var pid = document.getElementById(parentId);
	var len = document.forms[0].getInputs('file').length;
	alert(len);
	pid.removeChild(vid);
	//var len = pid.childNodes;

	if (maxSize != -1 && len >= maxSize){
        document.getElementById('a_trafficImage_add').disabled = true;
    }
}



/*
 * 初始单个图片类型上传方法
 * fileEl:   file input element
 * list_target:  文件显示列表
 * max : 上传文件的最大数
 *
 */
function initFileInput(fileEl,list_target, max,imgTy){
	/* Create an instance of the multiSelector class, pass it the output target and the max number of files */
		var multi_selector = new MultiSelector(list_target,0, max,imgTy,fileEl.id);
		/*Pass in the file element*/
		multi_selector.addElement(fileEl);
		return multi_selector;

}

function initFileInput(fileEl,list_target,count, max,imgTy){
	/* Create an instance of the multiSelector class, pass it the output target and the max number of files */
		var multi_selector = new MultiSelector(list_target,count, max,imgTy,fileEl.id);
		/*Pass in the file element*/
		multi_selector.addElement(fileEl);
		return multi_selector;

}


function addvalidate(formId){

	var valid = new Validation(formId, {onSubmit:true,immediate : true});

}

function preview(filepath){
	var url = PROJECT_NAME_PATH_MYCE + "/common_commonAction.do?method=initImagePreview&filePath=" + filepath;

	windowOpen(url,"layOutTrafficImage",'预览',600,490);
}
/*
 * 文件上传页面操作对象
 */

function MultiSelector(list_target,max,imgTy,elId){
	//form id
	this.uploadForm = document.forms[0].id;
	//elment id
	this.elmentId = elId;
	//element valid class
	this.validClass = "";
    // Where to write the list
    this.list_target = list_target;
    // How many elements?
    this.count = 0;
    //图片类型(包括模块类型:prjDoortype_doortype为户型管理的户型图)
    this.imgType = imgTy;
    // How many elements?
    this.id = 0;
	//新增的file集合
	//this.newEleArr=[];
    // Is there a maximum?
    if (max){
        this.max = max;
    }else{
        this.max = -1;
    };
    /**
     * Add a new file input element
     */
    this.addElement = function(element){
		// Make sure it's a file input element
        if (element.tagName == 'INPUT' && element.type == 'file'){
			// Element name -- what number am I?
            element.name = 'file_' + (this.id++)+'_'+this.imgType;
			//alert(element.name);
            // Add reference to this object
            element.multi_selector = this;
            // What to do when a file is selected
            element.onchange = function(){

				//alert(element.name+":"+element.value);
				if(this.multi_selector.list_target.tagName=="DIV"){
					// New file input
                	var new_element = document.createElement('input');
                	new_element.type = 'file';
                	new_element.size = 1;
                	new_element.className = "addfile";
                	// Add new element
                	this.parentNode.insertBefore(new_element, this);
                	// Apply 'update' to element
                	this.multi_selector.addElement(new_element);
					//var valid = this.multi_selector.addvalidate("addDoorTyForm");
					element.blur();
                	// Update list
                	this.multi_selector.addListRow(this);
				}else{
					 this.multi_selector.list_target.value = this.value;
				}
                // Hide this: we can't use display:none because Safari doesn't like it
                this.style.position = 'absolute';
                this.style.left = '-1000px';
				//this.multi_selector.newEleArr[this.multi_selector.newEleArr.length] = element;
                //clipboardData.setData("text", document.getElementById("formDiv").innerHTML);
            };
            // If we've reached maximum number, disable input element
            if (this.max != -1 && this.count >= this.max){
                element.disabled = true;
            };

            // File element counter
            this.count++;
            // Most recent element
            this.current_element = element;
			//alert(element.name+":"+element.value);
        }
        else{
            // This can only be applied to file input elements!
            alert('Error: not a file input element');
        };
    };


    /**
     * Add a new row to the list of files
     */
    this.addListRow = function(element){
		// Row div
        var new_row = document.createElement('div');
		new_row.className="inputDiv";
        //build Delete button
        var new_row_button = this.buildImageDelete(element);
		//build  a hyperlink
		var new_a_href = this.buildAhref(element);
        // References
        new_row.element = element;
       //.substring(element.value.lastIndexOf("\\")+1)
		new_a_href.innerHTML = element.value.substring(element.value.lastIndexOf("\\")+1)+ " ";
        new_row.appendChild(new_a_href);
        // Add button
        new_row.appendChild(new_row_button);
        // Add it to the list
        this.list_target.appendChild(new_row);
    };
/*******private method**********/
	/**
	 * build a for look image
	 */
	this.buildAhref = function(element){
		var new_a_href = document.createElement('a');
		new_a_href.href = "#";
		new_a_href.onclick = function(){
			preview(element.value);
		}
		return new_a_href;
	}
	/**
	 *build a image for delete
	 */
    this.buildImageDelete =  function(element){
		var new_row_button = document.createElement('img');
		new_row_button.src = "images/ico_delete.gif";
        // Delete function
        new_row_button.onclick = function(){
             //alert(" element="+this.parentNode.element.toString());
            // Remove element from form
			//alert(this.parentNode.element.name+":"+this.parentNode.element.value);
            this.parentNode.element.parentNode.removeChild(this.parentNode.element);
            // Remove this row from the list
            this.parentNode.parentNode.removeChild(this.parentNode);
            // Decrement counter
			var temp_multi_selector = this.parentNode.element.multi_selector;
            var fileCount = temp_multi_selector.count--;
			var temp_class = temp_multi_selector.validClass;
            // Re-enable input element (if it's disabled)
			var last_element = this.parentNode.element.multi_selector.current_element;
			if(document.getElementById(temp_multi_selector.elmentId)==null) {
				last_element.id=temp_multi_selector.elmentId;
			}
            last_element.disabled= false;

			if(temp_class!=""){
				if(fileCount <= 2){
					last_element.className=last_element.className+" "+temp_class;
	            	addvalidate(this.parentNode.element.multi_selector.uploadForm);
					last_element.focus();
					last_element.blur();
				}
			}
			// Appease Safari
            // without it Safari wants to reload the browser window
            // which nixes your already queued uploads
            return false;
        };
	 return new_row_button;
	}
};

